<%@page import="ca.uvic.group2.ui.*"%>
<%@page import="ca.uvic.group2.api.*"%>
<%@page import="java.util.*"%>


<html>
<head>
<title>StockServ Main Menu</title>
<style>
body
{
        background-color: white;
}

form
{
        margin: 0px;
        padding: 0px;
}

.loginform
{
        border: 1px solid black;
        background-color: #ccc;
        width: 80%;
        margin-left: 10%;
        margin-right: 10%;
        margin-top: 10px;
}
.errorMessage
{
        border: 1px solid #990;
        background-color: #FFA;
        margin: 5px;
}
</style>

<script>
function setFocus() { }
</script>
</head>

<body onload="setFocus();">
<h1>StockServ Debug Menu</h1>

<!---------------------- USER INFO  ----------------------->

<div class='loginform'>

<% 
   Object message = request.getAttribute("message");
   if (message != null) {
      out.print("<center><div class='errorMessage'>");
      out.print(message.toString());
      out.print("</div></center>");
   }
%>

<%
   UserInfo userInfo = (UserInfo)request.getAttribute("userInfo");
%>
<center><table>
        <tr>
                <th colspan=2>User Info</th>
        </tr>
        <tr>
                <td>User Name:</td>
                <td><% out.print(userInfo.username); %></td>
        </tr>
        <tr>
                <td>Balance:</td>
                <td><% out.print(String.valueOf(userInfo.balance)); %></td>
        </tr>
        <tr>
                <td>Reserve Balance:</td>
                <td><% out.print(String.valueOf(userInfo.reserveBalance)); %></td>
        </tr>
        <% if (!userInfo.stocks.isEmpty()) { %>
           <tr>
                     <th colspan=2>Current Stocks</th>
           </tr>
           <% Iterator i = userInfo.stocks.values().iterator();
              while (i.hasNext()) { 
                    StockRecord stock = (StockRecord)i.next();  %>
                    <tr><td><% out.print(stock.stock); %></td>
                    <td><% out.print(String.valueOf(stock.amount)); %> shares.</td></tr>
           <% }  %>
         <% } %>
</table></center>
</div>
<!---------------------- END USER INFO  ----------------------->


<!---------------------- PENDING BUYS LIST  ----------------------->
<% if (!userInfo.pendingBuys.isEmpty()) { %>
   
<div class='loginform'>
<center>
<table border=1>
  <tr><th colspan=5>Pending Purchases</th></tr>
  <tr><th>Stock Name</th><th>Price</th><th>Amount</th><th>Total</th><th>Valid For:</th></tr>
<% for (Iterator i = userInfo.pendingBuys.iterator(); i.hasNext(); ) {
     PurchaseOrder order = (PurchaseOrder)i.next(); 
%>
  <tr>
    <td><% out.print(order.quote.stock); %></td>
    <td>$<% out.print(String.valueOf(order.quote.price)); %></td>
    <td><% out.print(String.valueOf(order.amount)); %></td>
    <td>$<% out.print(String.valueOf(order.total)); %></td>
    <td><% int sec = ((int)(order.expiry - System.currentTimeMillis())) / 1000;
if (sec > 0) { 
        out.print(String.valueOf(sec) + " sec"); 
} else { 
        out.print("Expired "); 
 %><form action='stockserv'>
        <input type='hidden' name='cmd' value='buy'>
        <input type='hidden' name='stock' value='<% out.print(order.quote.stock); %>'>
        <input type='hidden' name='amount' value='<% out.print(String.valueOf(order.total)); %>'>
        <input type='submit' value='Re-submit'>
   </form> <%
}

 %></td>
  </tr>
<% } %>
   <tr><td></td><td></td><td></td><td>
        <form action='stockserv'>
          <input type='hidden' name='cmd' value='cancelbuy'>
          <input type='submit' value='Cancel'>
        </form>
        </td><td>
        <form action='stockserv'>
          <input type='hidden' name='cmd' value='commit'>
          <input type='submit' value='Commit'>
        </form>
   </td></tr>
</table>
</center>
</div>

<% } %>

<!---------------------- PENDING END BUYS LIST  ----------------------->


<br>

<!---------------------- PENDING SELLS LIST  ----------------------->

<% if (!userInfo.pendingSells.isEmpty()) { %>
   
<div class='loginform'>
<center>
<table border=1>
  <tr><th colspan=5>Pending Sales</th></tr>
  <tr><th>Stock Name</th><th>Price</th><th>Amount</th><th>Total</th><th>Valid For:</th></tr>
<% for (Iterator i = userInfo.pendingSells.iterator(); i.hasNext(); ) {
     PurchaseOrder order = (PurchaseOrder)i.next(); 
%>
  <tr>
    <td><% out.print(order.quote.stock); %></td>
    <td>$<% out.print(String.valueOf(order.quote.price)); %></td>
    <td><% out.print(String.valueOf(order.amount)); %></td>
    <td>$<% out.print(String.valueOf(order.total)); %></td>
    <td><% int sec = ((int)(order.expiry - System.currentTimeMillis())) / 1000;
           if (sec > 0) { out.print(String.valueOf(sec) + " sec"); } else { out.print("Expired"); } %></td>
  </tr>
<% } %>
   <tr><td></td><td></td><td></td><td>
        <form action='stockserv'>
          <input type='hidden' name='cmd' value='cancelsell'>
          <input type='submit' value='Cancel'>
        </form>
        </td><td>
        <form action='stockserv'>
          <input type='hidden' name='cmd' value='commitsell'>
          <input type='submit' value='Commit'>
        </form>
   </td></tr>
</table>
</center>
</div>

<% } %>

<!---------------------- PENDING SELLS LIST  ----------------------->

<!---------------------- BUY TRIGGERS LIST ------------------->

<% if (!userInfo.buyTriggers.isEmpty()) { %>
   
<div class='loginform'>
<center>
<form action='stockserv' name='buyTriggerForm' id='buyTriggerForm'>
<input type='hidden' name='cmd' value='cancelbuytrigger'>
<input type='hidden' name='stock' value=''>
<table border=1>
  <tr><th colspan=4>Buy Triggers</th></tr>
  <tr><th>Stock Name</th><th>Amount</th><th>Stock Price</th></tr>
<% for (Iterator i = userInfo.buyTriggers.values().iterator(); i.hasNext(); ) {
     Trigger trigger = (Trigger)i.next(); 
%>
  <tr>
    <td><% out.print(trigger.stock); %></td>
    <td>$<% out.print(String.valueOf(trigger.amount)); %></td>
    <td>$<% out.print(String.valueOf(trigger.stockPrice)); %></td>
    <td><input type='submit' 
               value="Cancel"
               onclick="document.buyTriggerForm.stock.value='<% out.print(trigger.stock); %>';"></td>
  </tr>
<% } // for %>
</table>
</form>
</center>
</div>

<% } // if %>

<!---------------------- END BUY TRIGGERS LIST ------------------->

<!---------------------- SELL TRIGGERS LIST ------------------->

<% if (!userInfo.sellTriggers.isEmpty()) { %>
   
<div class='loginform'>
<center>
<form action='stockserv' name='sellTriggerForm' id='sellTriggerForm'>
<input type='hidden' name='cmd' value='cancelselltrigger'>
<input type='hidden' name='stock' value=''>
<table border=1>
  <tr><th colspan=4>Sell Triggers</th></tr>
  <tr><th>Stock Name</th><th>Amount</th><th>Stock Price</th></tr>
<% for (Iterator i = userInfo.sellTriggers.values().iterator(); i.hasNext(); ) {
     Trigger trigger = (Trigger)i.next(); 
%>
  <tr>
    <td><% out.print(trigger.stock); %></td>
    <td><% out.print(String.valueOf((int)trigger.amount)); %></td>
    <td>$<% out.print(String.valueOf(trigger.stockPrice)); %></td>
    <td><input type='submit' 
               value="Cancel"
               onclick="document.sellTriggerForm.stock.value='<% out.print(trigger.stock); %>';"></td>
  </tr>
<% } // for %>
</table>
</form>
</center>
</div>

<% } // if %>

<!---------------------- END SELL TRIGGERS LIST ------------------->


<br>

<!---------------------- STOCK QUOTE INPUT ----------------------->

<center>
<div class='loginform'>
<form action='stockserv'>Stock Quote:
  <input type='TEXT' name='stock'>
  <input type='SUBMIT' value='Get Quote'>
  <input type='HIDDEN' name='cmd' value='quote'>
</form>
</div>

<!---------------------- END STOCK QUOTE INPUT ----------------------->

<!---------------------- BUY STOCK INPUT ----------------------->

<div class='loginform'>
<form action='stockserv' name="buyForm" id="buyForm">
  <table>
    <tr><th>Buy Stocks:</th><td>Stock:</td><td><input type='TEXT' name='stock'></td><td></td></tr>
    <tr><td></td><td>Amount: $</td><td><input type='TEXT' name='amount'</td>
        <td><input type='SUBMIT' value='Buy' 
                   onclick="document.buyForm.cmd.value='buy';"><input type='HIDDEN' name='cmd' value='buy'></td></tr>
    <tr><td></td><td></td><td></td><td><input type='SUBMIT' value='Add Trigger' 
                onclick="document.buyForm.cmd.value='buytrigger';"></td></tr>      
  </table>
</form>
</div>

<!---------------------- END BUY STOCK INPUT ----------------------->

<!---------------------- SELL STOCK INPUT ----------------------->

<div class='loginform'>

<form action='stockserv' name="sellForm" id="sellForm">
  <table>
    <tr><th>Sell Stocks:</th><td>Stock:</td><td><input type='TEXT' name='stock'></td><td></td></tr>
    <tr><td></td><td>Number to Sell: </td><td><input type='TEXT' name='amount'</td>
    <td><input type='SUBMIT' value='Sell'
               onclick="document.sellForm.cmd.value='sell';"><input type='HIDDEN' name='cmd' value='sell'></td></tr>
    <tr><td></td><td></td><td></td><td><input type='SUBMIT' value='Add Trigger' 
                onclick="document.sellForm.cmd.value='selltrigger';"></td></tr>      
  </table>
</form>
</div>
<!---------------------- END SELL STOCK INPUT ----------------------->

<!---------------------- ADD FUNDS INPUT ----------------------->

<div class='loginform'>


<form action='stockserv'>
  <table>
    <tr><th>Add Funds:</th><td>Amount: $</td><td><input type='TEXT' name='amount'></td><td></td></tr>
    <td><input type='SUBMIT' value='Add Funds'><input type='HIDDEN' name='cmd' value='addfunds'></td></tr>
  </table>
</form>
</div>
<!---------------------- END ADD FUNDS INPUT ----------------------->

<!---------------------- DUMPLOG INPUT ----------------------->
<div class='loginform'>
<form action='stockserv' name='dumplogform'>
<input type='HIDDEN' name='cmd' value='dumplog'>        
<input type='HIDDEN' name='user' value='0'>      
  <table>
    <tr><th>Log Files: </th>
    <td><input type='submit' value='All user log' onclick='document.dumplogform.user.value="0";'></td>
    <td><input type='submit' value='Your log' onclick='document.dumplogform.user.value="1";'></td></tr>
  </table>
</form>
</div>
<!---------------------- END DUMPLOG INPUT ----------------------->

<!---------------------- LOGOUT INPUT ----------------------->
<div class='loginform'>
<form action='stockserv'><input type='SUBMIT' value='Logout'><input type='HIDDEN' name='cmd' value='logout'>
</form>
</div>
<!---------------------- END LOGOUT INPUT ----------------------->


</center>
</body>
</html>
